#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author: Florian Dorn <florian.dorn@gmail.com>
# @package: agenda
# @license: GNU GPLv2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import gtk
import gobject
from gettext import gettext as _

class AgendaWindow (gtk.Window):
	size = None
	"""
	Window aligning itself to a status Icon
	"""
	def on_size_change(self,window,size):
		if not self.window:
			return
		screen = self.statusIcon.get_screen()
		screen_size = screen.get_monitor_geometry (screen.get_monitor_at_window(self.window))
		if screen_size > size: 
			self.update_position()
			

	def __init__(self, statusIcon, type=gtk.WINDOW_TOPLEVEL):
		self.size = None
		"""
		Call AgendaWindow.update_position () to position the window.
		"""
		gtk.Window.__init__(self, type)
		#self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_UTILITY)
		#self.set_decorated (False)
		
		# Skip the taskbar, and the pager, stick and stay on top
		self.stick()
		#self.set_keep_above(True)
		#self.set_skip_pager_hint(True)
		#self.set_skip_taskbar_hint(True)
		#self.set_border_width(0)
		#self.set_gravity(gtk.gdk.GRAVITY_SOUTH_EAST)

		#self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_MENU)
				
		
		self.statusIcon = statusIcon
		
		self.realize_status = None
		
		#self.connect ("realize", lambda win : self.__register_realize ())
		self.connect ("delete-event", self.on_delete_event)
		#self.connect_after ("size-allocate",self.on_size_change)

		try:
			(screen, area, alignment) = self.statusIcon.get_geometry()
			self.support_alignment = True
			#print "Does support alignment"
		except:
			self.support_alignment = False
			self.set_decorated(True)
			self.set_border_width(0)
			
		self.set_title(_("Agenda"))
		self.size = self.get_size()
		#self.set_decorated(False)
			
	def on_delete_event(self, window, event):
		# Since all users of this class expect it to remain open and alive
		# We must catch deletion events (for example alt-f4) and just hide the window
		self.hide()
		return True

	def show_window(self):
		if(not self.get_property("visible")):
			self.update_position()
			self.show()
			return True
		else:
			if self.get_property("type") == gtk.WINDOW_TOPLEVEL:
				if(not self.is_active()):
					self.present()
					return True
				else:
					self.hide()
					return False
			else:
				self.hide()
				return False
		return False
	def toggle_window(self):
		
		if self.get_property("visible"):
			self.hide()
		else:
			self.show_window()
	def update_position (self):
		"""
		Calculates the position and moves the window to it.
		IMPORATNT: widgetToAlignWith should be realized!
		"""
		# Get our own dimensions & position

		if(self.support_alignment):
			(screen, area, alignment) = self.statusIcon.get_geometry()
			#print (screen, area, alignment)
		
			#pos of the status_icon
			(ax, ay) = (area.x,area.y)
			#window size
			(ww, wh) = self.get_size ()	
			#ww,wh = self.size
			
			(aw, ah) = (area.width,area.height)

			monitor_geom = screen.get_monitor_geometry (screen.get_monitor_at_point (ax,ay))
			x=0
			y=0
			
			if alignment == gtk.ORIENTATION_HORIZONTAL:
				x = ax
				y = ay
				if (y + wh > monitor_geom.y + monitor_geom.height):
					y = monitor_geom.y + monitor_geom.height - wh - ah
				else:
					y += ah
				
				if (y < 0):
					y = 0

						
			elif alignment == gtk.ORIENTATION_VERTICAL:
				x = ax
				y = ay + ah
				
				if (x + ww > monitor_geom.x + monitor_geom.width):
					x = monitor_geom.x + monitor_geom.width - ww
					
				if (x < 0):
					x = 0

			e_x,e_y = (x+ww,y+wh)
			if e_x > monitor_geom.width:
				x -= e_x - monitor_geom.width
			if e_y > monitor_geom.height:
				y -= e_y - monitor_geom.height
			
		else:
			"""The platform does  not support statusIcon.get_geometry()"""
			x,y = (50,50)
		#print x,y
		self.move(x, y)
	def __register_realize (self):
		self.realize_status = True
		self.update_position()
		
gobject.type_register (AgendaWindow)
